﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Fierce.Utility.Helper;
using Fierce.Entity.Basic;
using Fierce.Core.Aop;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Authorization;

namespace Fierce.MvcExtend.Controllers
{
    public abstract class WorkController : Controller
    {
        private const string SessionUserIDKey = "FIERCE-MANAGE-USERID";
        private const string SessionUserNameKey = "FIERCE-MANAGE-USERNAME";
        private const string SessionNameKey = "FIERCE-MANAGE-NAME";
        private const string SessionIsSuperKey = "FIERCE-MANAGE-ISSUPER";
        private const string SessionUserRoleKey = "FIERCE-MANAGE-USERROLE";

        /// <summary>
        /// 用户编号
        /// </summary>
        public string UserID
        {
            get { return HttpContext.Session.Get<string>(SessionUserIDKey); }
            set { HttpContext.Session.Set<string>(SessionUserIDKey, value); }
        }

        /// <summary>
        /// 用户账号
        /// </summary>
        public string UserName
        {
            get { return HttpContext.Session.Get<string>(SessionUserNameKey); }
            set { HttpContext.Session.Set<string>(SessionUserNameKey, value); }
        }

        /// <summary>
        /// 用户姓名
        /// </summary>
        public string Name
        {
            get { return HttpContext.Session.Get<string>(SessionNameKey); }
            set { HttpContext.Session.Set<string>(SessionNameKey, value); }
        }

        /// <summary>
        /// 超级管理
        /// </summary>
        public bool IsSuper
        {
            get { return HttpContext.Session.Get<bool>(SessionIsSuperKey); }
            set { HttpContext.Session.Set<bool>(SessionIsSuperKey, value); }
        }

        /// <summary>
        /// 用户角色
        /// </summary>
        public string UserRole
        {
            get { return HttpContext.Session.Get<string>(SessionUserRoleKey); }
            set { HttpContext.Session.Set<string>(SessionUserRoleKey, value); }
        }

        /// <summary>
        /// 用户验证
        /// </summary>
        public bool Authenticated
        {
            get { return !string.IsNullOrEmpty(UserID); }
        }

        /// <summary>
        /// 管理验证
        /// </summary>
        public bool ManageAuthenticated
        {
            get { return UserRole == "MANAGE" && !string.IsNullOrEmpty(UserID); }
        }

        /// <summary>
        /// 注销登录
        /// </summary>
        public void LoginOut()
        {
            HttpContext.Session.Clear();
        }

        /// <summary>
        /// 设置登录
        /// </summary>
        public void SetLoginSession(BasicUser model)
        {
            UserID = model.ID;
            UserName = model.UserName;
            Name = model.Name;
            IsSuper = model.IsSuper;
            UserRole = "MANAGE";
        }

        /// <summary>
        /// 权限验证
        /// </summary>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!ManageAuthenticated)
            {
                if (context.ActionDescriptor.FilterDescriptors.Count(m => m.Filter is AllowAnonymousFilter) == 0)
                {
                    if (ConfigHelper.IsDevelop)
                    {
                        var result = ServiceFactory.BasicUser.Login("zhcnlei", "leizhen");

                        if (result.Code == ServiceResultCode.Success)
                        {
                            SetLoginSession(result.Data as BasicUser);
                        }
                    }
                    else { context.Result = LoginResult(context); }
                }
            }

            ViewBag.Work = this;

            base.OnActionExecuting(context);
        }

        /// <summary>
        /// 登录结果
        /// </summary>
        private IActionResult LoginResult(ActionExecutingContext context)
        {
            var cad = context.ActionDescriptor as ControllerActionDescriptor;

            if (cad.MethodInfo.ReturnType == typeof(IActionResult))
            {
                return Redirect("/work/authorize/index");
            }

            return Content(ServiceResult.Serialize(ServiceResultCode.LoginTimeout));
        }
    }
}